package com.epoint.pjq.core.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

public class MusicService extends Service {
	// 为日志工具设置标签
	private static String TAG = "andli";
	// 定义音乐播放器变量
	private MediaPlayer mPlayer;

	int filename = 0;

	// 该服务不存在需要被创建时被调用，不管startService()还是bindService()都会启动时调用该方法
	@Override
	public void onCreate() {
		Log.i(TAG, "MusicSerice onCreate()");
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.i(TAG, "MusicSerice onStart()");

		filename = intent.getIntExtra("filename", 0);
		Log.i(TAG, "filename" + filename);
		if (filename != 0) {
			mPlayer = MediaPlayer.create(getApplicationContext(), filename);
			// 设置可以重复播放
			// mPlayer.setLooping(true);
			mPlayer.start();
		}

		super.onStart(intent, startId);
	}

	@Override
	public void onDestroy() {
		Log.i(TAG, "MusicSerice onDestroy()");

		if (mPlayer != null) {
			mPlayer.stop();
		}

		super.onDestroy();
	}

	// 其他对象通过bindService 方法通知该Service时该方法被调用
	@Override
	public IBinder onBind(Intent intent) {
		Log.i(TAG, "MusicSerice onBind()");
		if (mPlayer != null) {
			mPlayer.start();
		}

		return null;
	}

	// 其它对象通过unbindService方法通知该Service时该方法被调用
	@Override
	public boolean onUnbind(Intent intent) {
		Log.i(TAG, "MusicSerice onUnbind()");
		if (mPlayer != null) {
			mPlayer.stop();
		}

		return super.onUnbind(intent);
	}

}